﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.Text.RegularExpressions;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace MobileSalesWS
{
    public class Utils
    {
        #region :: Constants ::

        public const string ROOT_START_TAG = "<DocumentElement>";
        public const string ROOT_END_TAG = "</DocumentElement>";

        #endregion :: Constants ::

        #region :: Public Functions ::

        public static SqlConnection OpenDBConnection(string loginID, string password)
        {
            string connString = ConfigurationManager.ConnectionStrings["GPDatabase"].ConnectionString;
            SqlConnectionStringBuilder connStringBuilder =
                new SqlConnectionStringBuilder(connString);
            connStringBuilder.UserID = loginID;
            connStringBuilder.Password = password;
            SqlConnection dbConn = new SqlConnection(connStringBuilder.ConnectionString);
            dbConn.Open();

            return dbConn;
        }

        public static string TransformXML(string xmlString, string xsltFilename)
        {
            string xslPath = HttpContext.Current.ApplicationInstance.Server.MapPath("~/App_Data");
            xslPath += "\\" + xsltFilename;

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(xmlString);

            XslCompiledTransform xsltDoc = new XslCompiledTransform();
            xsltDoc.Load(xslPath);
            
            System.Text.StringBuilder xsltSB = new System.Text.StringBuilder();
            System.IO.StringWriter xsltSW = new System.IO.StringWriter(xsltSB);
            XmlTextWriter writer = new XmlTextWriter(xsltSW);
            writer.Formatting = Formatting.None;
            
            xsltDoc.Transform(new XmlNodeReader(xmlDoc), writer);

            xsltSW.Close();
            writer.Close();

            string transformedXML = xsltSB.ToString();

            // Remove newlines
            transformedXML = transformedXML.Replace("\n", "");

            string newXMLStr = string.Empty;
            for (int i = transformedXML.Length; i > 0; i--)
            {
                if ((transformedXML[i - 1] == '/') && (transformedXML[i - 2] == '<'))
                {
                    newXMLStr = transformedXML[i - 1] + newXMLStr;
                    i--;
                    newXMLStr = transformedXML[i - 1] + newXMLStr;
                    i--;
                    while ((i > 0) && (transformedXML[i - 1] == ' '))
                    {
                        i--;
                    }
                    if (i > 0)
                    {
                        newXMLStr = transformedXML[i - 1] + newXMLStr;
                    }
                }
                else
                {
                    newXMLStr = transformedXML[i - 1] + newXMLStr;
                }
            }

            return newXMLStr;
        }

        #endregion :: Public Functions ::
    }
}